﻿

Public Class LoginForm1

    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.
    Public MainForm As Form1
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try

        Dim con As New Odbc.OdbcConnection
        Dim reader As Odbc.OdbcDataReader

        con.ConnectionString = My.Settings.con
        Dim cmd As New Odbc.OdbcCommand("SELECT * FROM users WHERE USERNAME = '" & UsernameTextBox.Text & "' AND PASSWORD = '" & PasswordTextBox.Text & "'", con)

        con.Open()
        reader = cmd.ExecuteReader()
        While reader.Read()
            If reader(0) <> Nothing Then
                My.Settings.username = reader(0)
                My.Settings.password = reader(1)
                My.Settings.email = reader(2)
                My.Settings.company = reader(3)
                My.Settings.department = reader(4)
                My.Settings.role = reader(5)
                My.Settings.Save()
                Me.Close()
                Exit Sub
            End If
        End While
            MsgBox("Login Failed")
        Catch ex As Exception
            MsgBox("Login Failed")
        End Try
        'Me.Close()
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Login " & ProductVersion
        My.Settings.Reload()
        UsernameTextBox.Focus()
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub LogoPictureBox_Click(sender As Object, e As EventArgs) Handles LogoPictureBox.Click

    End Sub

    Private Sub LoginForm1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Me.Text = "Login " & ProductVersion
        My.Settings.Reload()
        UsernameTextBox.Focus()
    End Sub
End Class
